package edu.uoc.tdp.pac4.client.logandreg;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.uoc.tdp.pac4.common.util.LanguageUtils;


/**
 * Class to change the interface language
 *
 */

public class ChangeLanguageWindow extends JPanel {


	private static final long serialVersionUID = 1L;
	private JButton btnAcceptar=null;
	private JComboBox comboBox = null;
	private JFrame mainFrame = null;
	
	
	public ChangeLanguageWindow(final JFrame frame) {
		mainFrame = frame;
		
		JPanel panel = new JPanel();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
					.addGap(146)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 181, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(123, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(103)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(77, Short.MAX_VALUE))
		);
		
		JLabel lblSelleccionarIdioma = new JLabel(LanguageUtils.getMessage("client.regandlog.label.lang"));
		panel.add(lblSelleccionarIdioma);
		comboBox = new JComboBox();
		panel.add(comboBox);
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"English", "Catal\u00E0", "Castellano"}));
		comboBox.setMaximumRowCount(3);
		comboBox.setAlignmentY(CENTER_ALIGNMENT);
		
		btnAcceptar = new JButton("Acceptar");
		panel.add(btnAcceptar);
		btnAcceptar.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				if(arg0.getSource() == btnAcceptar){
					String idioma = (String)comboBox.getSelectedItem();
					if(idioma != null && !"".equals(idioma)){
						Locale aux = null;
						if(idioma.equals("English")){
							aux = new Locale("en", "US");
						}else if(idioma.equals("Catal\u00E0")){
							aux = new Locale("ca", "ES");
						}else if(idioma.equals("Castellano")){
							aux = new Locale("es", "ES");
						}
						if(aux != null){
							LanguageUtils.setLanguage("i18n/messages", aux);
							mainFrame.repaint();
							System.out.println("Selected locale: "+ aux.toString());
						}
					}
				}
				
			}
		});
		setLayout(groupLayout);

	}
}
